<template>
  <div class="roles">
      <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 视图 -->
    <el-card>
      <el-row> <el-button type="primary">
        添加角色
      </el-button><el-col></el-col></el-row> 
      <!-- 列表 -->
      <el-table :data="roleList" style="width: 100%" border stripe>
        <!-- 展开列 -->
        <el-table-column type='expand'>
          <template slot-scope="scope">
            <el-row v-for="(item1,index1) in scope.row.children" :key='item1.id' :class="['bdbottom',index1===0?'bdtop':'','vcenter']"> 
              <!-- 一级权限 -->
              <el-col :span='5'>
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二三 --> 
              <el-col :span='19'>
                <!-- 二级 -->
                 <el-row v-for="(item2,index2) in item1.children" :key='item2.id' :class="[index2===0?'':'bdtop','vcenter']">
                    <el-col :span='6'>
                    <el-tag type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                     </el-col>
                     <el-col :span='18'>
                        <!-- 三级 -->
                         <el-tag type="warning" v-for="(item3) in item2.children" :key='item3.id' closable @close="removebyid(scope.row,item3.id)">
                           {{item3.authName}}

                         </el-tag>
                       </el-col>
                   </el-row>
              </el-col>
            </el-row>
           <!--  <pre>
              {{scope.row}}
            </pre> -->
          </template>
        </el-table-column>
        <!-- 索引列 -->
         <el-table-column type='index'></el-table-column>
         <el-table-column prop="roleName" label="角色名称" ></el-table-column>
         <el-table-column prop="roleDesc" label="描述" ></el-table-column>
         <el-table-column  label="操作" width="300px">
          <template slot-scope="scope">
             <el-button size='mini' type="primary" icon="el-icon-edit">编辑</el-button>
             <!-- 删除 -->
             <el-button size='mini' type="danger" icon="el-icon-delete" >删除</el-button>
             <el-button size='mini' type="warning" icon="el-icon-setting" @click="setDialog(scope.row)">分配权限</el-button>  
          </template>
         </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="setdialogVisible" width="50%" @close="closeddialog">
     <el-tree :data="setdialog" :props="setProps" show-checkbox node-key="id" :default-expand-all="true" :default-checked-keys="defaultkey" ref='eltree'></el-tree>
     <span slot="footer" class="dialog-footer">
    <el-button @click="setdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotright">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
data(){

  return{
    roleList:[],
    setdialogVisible:false,//分配角色对话框隐藏
    setdialog:{},
    //树形控件
    setProps: {
          children: 'children',
          label: 'authName'
        },
    defaultkey:[],//默认选中的id
    roleid:''//即将分配权限的id
  }
},
created(){
  this.getRoleList()
},
methods:{
  getRoleList(){
    this.$http.get(`roles`).then(response => {
      console.log(response)
      let res = response.data
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败');
      this.roleList = res.data
    })
  },
  /* 删除权限 */
    async removebyid(role,id){
   let confirm= await  this.$confirm('此操作将永久删除权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        if (confirm !== 'confirm') {
          return this.$message.info('取消删除')
        }
       let {data:res} = await this.$http.delete(`roles/${role.id}/rights/${id}`)
       if (res.meta.status !== 200) return this.$message.error('删除权限失败');
       /* 防止重新请求渲染页面 */
       role.children = res.data
        this.$message.info('确认删除')
  },
  /* 分配权限对话框 */
  async setDialog(role){
    this.roleid=role.id
    /* 获取全部权限列表 */
    let {data:res} = await this.$http.get(`rights/tree`)
       if (res.meta.status !== 200) return this.$message.error('获取权限失败');
       this.setdialog = res.data
       this.getdefaultkey(role,this.defaultkey)
        this.setdialogVisible = true
  },
  /* 递归获取三级权限id */
  getdefaultkey(node, arr){
    if (!node.children) {//不包含children就是三级节点
     return arr.push(node.id)
    }
    node.children.forEach(element => {
      this.getdefaultkey(element,arr)
    });
  },
  /* 关闭对权限对话框的事件 */
  closeddialog(){
    this.defaultkey = []
  },
  /* 分配权限 */
    async allotright(){
      let keys = [...this.$refs.eltree.getCheckedKeys(),...this.$refs.eltree.getHalfCheckedKeys()]
      console.log(keys)
      let keystr = keys.join(',')
      let {data:res} = await this.$http.post(`roles/${this.roleid}/rights`,{rids:keystr})
       if (res.meta.status !== 200) return this.$message.error('分配权限失败');
       this.$message.error('分配权限成功');
       this.getRoleList()
        this.setdialogVisible = false

  }
}
}
</script>

<style lang="less" scoped>
.bdbottom{
  border-bottom: 1px solid #eee;
}
.bdtop{
  border-top: 1px solid #eee;
}
.el-tag{
  margin: 7px;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>